package cn.kgc.reflect.base;

import cn.kgc.pojo.Book;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;

public class InterseptorDemo {

    public static void main(String[] args) throws Exception {
        // 内省 API
        BeanInfo info= Introspector.getBeanInfo(Book.class);




        //属性描述器
        PropertyDescriptor [] pds=info.getPropertyDescriptors();
        //方法描述器
        MethodDescriptor mds []= info.getMethodDescriptors();
        Class b1=Class.forName("cn.kgc.pojo.Book");
        for(PropertyDescriptor pd:pds){

            Object obj = b1.newInstance();
            if(pd.getName().equals("bookname")) {
                pd.getWriteMethod().invoke(obj, "入门放弃一步之遥");
                System.out.println(pd.getName());
                System.out.println(pd.getReadMethod().invoke(obj));
            }
            if (pd.getName().equals("author")){
                pd.getWriteMethod().invoke(obj,"chenyicheng");
                System.out.println(pd.getName());
                System.out.println(pd.getReadMethod().invoke(obj));
            }

            try {
                if (pd.getName().equals("stock")) {
                    pd.getWriteMethod().invoke(obj, 2);
                    System.out.println(pd.getName());
                    System.out.println(pd.getReadMethod().invoke(obj));
                }
            } catch (IllegalArgumentException e) {
                System.out.println("2");
            }

            //  commons-beanutils
        }

    }

}
